<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This file documents the use of the GNU compilers.

Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>x86 transactional memory intrinsics (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="x86 transactional memory intrinsics (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="x86 transactional memory intrinsics (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Indices.html" rel="index" title="Indices">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Target-Builtins.html" rel="up" title="Target Builtins">
<link href="x86-control_002dflow-protection-intrinsics.html" rel="next" title="x86 control-flow protection intrinsics">
<link href="x86-Built_002din-Functions.html" rel="prev" title="x86 Built-in Functions">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
kbd.key {font-style: normal}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>


</head>

<body lang="en_US">
<div class="subsection-level-extent" id="x86-transactional-memory-intrinsics">
<div class="nav-panel">
<p>
Next: <a href="x86-control_002dflow-protection-intrinsics.html" accesskey="n" rel="next">x86 Control-Flow Protection Intrinsics</a>, Previous: <a href="x86-Built_002din-Functions.html" accesskey="p" rel="prev">x86 Built-in Functions</a>, Up: <a href="Target-Builtins.html" accesskey="u" rel="up">Built-in Functions Specific to Particular Target Machines</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="x86-Transactional-Memory-Intrinsics"><span>6.60.36 x86 Transactional Memory Intrinsics<a class="copiable-link" href="#x86-Transactional-Memory-Intrinsics"> &para;</a></span></h4>

<p>These hardware transactional memory intrinsics for x86 allow you to use
memory transactions with RTM (Restricted Transactional Memory).
This support is enabled with the <samp class="option">-mrtm</samp> option.
For using HLE (Hardware Lock Elision) see 
<a class="ref" href="x86-specific-memory-model-extensions-for-transactional-memory.html">x86-Specific Memory Model Extensions for Transactional Memory</a> instead.
</p>
<p>A memory transaction commits all changes to memory in an atomic way,
as visible to other threads. If the transaction fails it is rolled back
and all side effects discarded.
</p>
<p>Generally there is no guarantee that a memory transaction ever succeeds
and suitable fallback code always needs to be supplied.
</p>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-_005fxbegin"><span class="category-def">RTM Function: </span><span><code class="def-type">unsigned</code> <strong class="def-name">_xbegin</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-_005fxbegin"> &para;</a></span></dt>
<dd><p>Start a RTM (Restricted Transactional Memory) transaction. 
Returns <code class="code">_XBEGIN_STARTED</code> when the transaction
started successfully (note this is not 0, so the constant has to be 
explicitly tested).  
</p>
<p>If the transaction aborts, all side effects
are undone and an abort code encoded as a bit mask is returned.
The following macros are defined:
</p>
<dl class="first-deffn first-defmac-alias-first-deffn">
<dt class="deffn defmac-alias-deffn" id="index-_005fXABORT_005fEXPLICIT"><span class="category-def">Macro: </span><span><strong class="def-name">_XABORT_EXPLICIT</strong><a class="copiable-link" href="#index-_005fXABORT_005fEXPLICIT"> &para;</a></span></dt>
<dd><p>Transaction was explicitly aborted with <code class="code">_xabort</code>.  The parameter passed
to <code class="code">_xabort</code> is available with <code class="code">_XABORT_CODE(status)</code>.
</p></dd></dl>

<dl class="first-deffn first-defmac-alias-first-deffn">
<dt class="deffn defmac-alias-deffn" id="index-_005fXABORT_005fRETRY"><span class="category-def">Macro: </span><span><strong class="def-name">_XABORT_RETRY</strong><a class="copiable-link" href="#index-_005fXABORT_005fRETRY"> &para;</a></span></dt>
<dd><p>Transaction retry is possible.
</p></dd></dl>

<dl class="first-deffn first-defmac-alias-first-deffn">
<dt class="deffn defmac-alias-deffn" id="index-_005fXABORT_005fCONFLICT"><span class="category-def">Macro: </span><span><strong class="def-name">_XABORT_CONFLICT</strong><a class="copiable-link" href="#index-_005fXABORT_005fCONFLICT"> &para;</a></span></dt>
<dd><p>Transaction abort due to a memory conflict with another thread.
</p></dd></dl>

<dl class="first-deffn first-defmac-alias-first-deffn">
<dt class="deffn defmac-alias-deffn" id="index-_005fXABORT_005fCAPACITY"><span class="category-def">Macro: </span><span><strong class="def-name">_XABORT_CAPACITY</strong><a class="copiable-link" href="#index-_005fXABORT_005fCAPACITY"> &para;</a></span></dt>
<dd><p>Transaction abort due to the transaction using too much memory.
</p></dd></dl>

<dl class="first-deffn first-defmac-alias-first-deffn">
<dt class="deffn defmac-alias-deffn" id="index-_005fXABORT_005fDEBUG"><span class="category-def">Macro: </span><span><strong class="def-name">_XABORT_DEBUG</strong><a class="copiable-link" href="#index-_005fXABORT_005fDEBUG"> &para;</a></span></dt>
<dd><p>Transaction abort due to a debug trap.
</p></dd></dl>

<dl class="first-deffn first-defmac-alias-first-deffn">
<dt class="deffn defmac-alias-deffn" id="index-_005fXABORT_005fNESTED"><span class="category-def">Macro: </span><span><strong class="def-name">_XABORT_NESTED</strong><a class="copiable-link" href="#index-_005fXABORT_005fNESTED"> &para;</a></span></dt>
<dd><p>Transaction abort in an inner nested transaction.
</p></dd></dl>

<p>There is no guarantee
any transaction ever succeeds, so there always needs to be a valid
fallback path.
</p></dd></dl>

<dl class="first-deftypefn">
<dt class="deftypefn" id="index-_005fxend"><span class="category-def">RTM Function: </span><span><code class="def-type">void</code> <strong class="def-name">_xend</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-_005fxend"> &para;</a></span></dt>
<dd><p>Commit the current transaction. When no transaction is active this faults.
All memory side effects of the transaction become visible
to other threads in an atomic manner.
</p></dd></dl>

<dl class="first-deftypefn">
<dt class="deftypefn" id="index-_005fxtest"><span class="category-def">RTM Function: </span><span><code class="def-type">int</code> <strong class="def-name">_xtest</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-_005fxtest"> &para;</a></span></dt>
<dd><p>Return a nonzero value if a transaction is currently active, otherwise 0.
</p></dd></dl>

<dl class="first-deftypefn">
<dt class="deftypefn" id="index-_005fxabort"><span class="category-def">RTM Function: </span><span><code class="def-type">void</code> <strong class="def-name">_xabort</strong> <code class="def-code-arguments">(status)</code><a class="copiable-link" href="#index-_005fxabort"> &para;</a></span></dt>
<dd><p>Abort the current transaction. When no transaction is active this is a no-op.
The <var class="var">status</var> is an 8-bit constant; its value is encoded in the return 
value from <code class="code">_xbegin</code>.
</p></dd></dl>

<p>Here is an example showing handling for <code class="code">_XABORT_RETRY</code>
and a fallback path for other failures:
</p>
<div class="example smallexample">
<pre class="example-preformatted">#include &lt;immintrin.h&gt;

int n_tries, max_tries;
unsigned status = _XABORT_EXPLICIT;
...

for (n_tries = 0; n_tries &lt; max_tries; n_tries++) 
  {
    status = _xbegin ();
    if (status == _XBEGIN_STARTED || !(status &amp; _XABORT_RETRY))
      break;
  }
if (status == _XBEGIN_STARTED) 
  {
    ... transaction code...
    _xend ();
  } 
else 
  {
    ... non-transactional fallback path...
  }
</pre></div>

<p>Note that, in most cases, the transactional and non-transactional code
must synchronize together to ensure consistency.
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="x86-control_002dflow-protection-intrinsics.html">x86 Control-Flow Protection Intrinsics</a>, Previous: <a href="x86-Built_002din-Functions.html">x86 Built-in Functions</a>, Up: <a href="Target-Builtins.html">Built-in Functions Specific to Particular Target Machines</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
